package com.winchain.chainbill.receivables.biz.impt.service.impl;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.ChainCallback;
import com.winchain.chainbill.chain.callback.CallbackService;
import com.winchain.chainbill.receivables.base.po.ArOperation;
import com.winchain.chainbill.receivables.base.po.ArRecord;
import com.winchain.chainbill.receivables.base.service.ArRecordService;
import com.winchain.chainbill.receivables.biz.impt.cmd.ReceivablesMoveCmd;
import com.winchain.chainbill.receivables.biz.impt.cmd.ReceivablesQuotaRollbackCmd;
import com.winchain.chainbill.trade.cmd.TranCallbackCmd;
import com.winchain.chainbill.trade.cmd.TranRollbackCmd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("receivablesImptCallbackService")
public class ReceivablesImptCallbackServiceImpl implements CallbackService {
	@Autowired
	private ArRecordService arRecordService;
	@Autowired
	private ReceivablesMoveCmd receivablesMoveCmd;
	@Autowired
	private ReceivablesQuotaRollbackCmd receivablesQuotaRollbackCmd;
	@Autowired
	private TranCallbackCmd<ArRecord> tranCallbackCmd;
	@Autowired
	private TranRollbackCmd<ArRecord> tranRollbackCmd;

	public String getType() {
		return "ar_impt";
	}

	public void confirm(AcctTxRec tran) {
		String arId = tran.getBizId();
		ArRecord record = (ArRecord) this.arRecordService.selectByKey(arId);

		ArOperation opt = new ArOperation();
		opt.setBizType(getType());
		opt.setState("I");
		opt.setOptCode("PS");
		opt.setNextState("1");
		opt.setRemark("����������������������������");
		opt.setNoTimeout(Boolean.valueOf(true));

		CallbackContext<ArRecord, AcctTxRec> context = new CallbackContext();
		context.setSource(record);
		context.setTarget(tran);
		context.setValue("Operation", opt);

		ChainCallback<ArRecord, AcctTxRec> ccb = new ChainCallback();
		ccb.add(this.receivablesMoveCmd);
		ccb.add(this.tranCallbackCmd);

		ccb.callback(context);
	}

	public void cancel(AcctTxRec tran) {
		String arId = tran.getBizId();
		ArRecord record = (ArRecord) this.arRecordService.selectByKey(arId);

		ArOperation opt = new ArOperation();
		opt.setBizType(getType());
		opt.setState("I");
		opt.setOptCode("RB");
		opt.setNextState("0");
		opt.setRemark("����������������������������");
		opt.setNoTimeout(Boolean.valueOf(true));

		CallbackContext<ArRecord, AcctTxRec> context = new CallbackContext();
		context.setSource(record);
		context.setTarget(tran);
		context.setValue("Operation", opt);

		ChainCallback<ArRecord, AcctTxRec> crb = new ChainCallback();
		crb.add(this.receivablesMoveCmd);
		crb.add(this.receivablesQuotaRollbackCmd);
		crb.add(this.tranRollbackCmd);

		crb.callback(context);
	}
}
